﻿using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using NCI.EasyObjects;
using Microsoft.Practices.EnterpriseLibrary;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data.Common;
using System.IO;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using CMS.BLL;


public class Support
{
    public static string GetCompanyName()
    {
        Company objCompany = new Company();
        DataTable dtbCompany = new DataTable();
        dtbCompany = objCompany.Search();
        string sCompanyName  = ConvertData.ConvertToString(dtbCompany.Rows[0]["CompanyName1"]);
        return sCompanyName;
    }
    public static string DeleteFile(string UploadType, string FileName)
    {
        string sDelFolder = "";
        switch (UploadType)
        {
            case "image":
                sDelFolder = Config.SLASH + Config.IMAGE_NEWS_DEFAULT_UPLOAD;
                break;
            case "Cruise":
                sDelFolder = Config.SLASH + Config.IMAGE_CRUISE_DEFAULT_UPLOAD;
                break;
            case "file":
                sDelFolder = Config.SLASH + Config.FILE_DEFAULT_UPLOAD;
                break;
            case "trademark":
                sDelFolder = Config.SLASH + Config.IMAGE_TRADEMARK_DEFAULT_UPLOAD;
                break;
            case "product":
                sDelFolder = Config.SLASH + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD;
                break;
            case "adv":
                sDelFolder = Config.SLASH + Config.IMAGE_ADV_DEFAULT_UPLOAD ;
                break;
            case "gallery":
                sDelFolder = Config.SLASH + Config.IMAGE_GALLERY_DEFAULT_UPLOAD;
                break;
            case "tourdestination":
                sDelFolder = Config.SLASH + Config.IMAGE_TOURDESTINATION_UPLOAD;
                break;
            case "tour":
                sDelFolder = Config.SLASH + Config.IMAGE_TOUR_UPLOAD;
                break;
            case "hotel":
                sDelFolder = Config.SLASH + Config.IMAGE_HOTEL_DEFAULT_UPLOAD;
                break;
            case "tourcat":
                sDelFolder = Config.SLASH + Config.IMAGE_TOURCAT_DEFAULT_UPLOAD;
                break;
            case "gallerydetail":
                string sFolder = Config.SLASH + Config.IMAGE_GALLERY_DEFAULT_UPLOAD;
                sDelFolder = sFolder + HttpContext.Current.Session[Config.SESSION_GALLERYID] + "/"; 
                break;
            case "staticpage":
                sDelFolder = Config.SLASH + Config.IMAGE_STATICPAGE_DEFAULT_UPLOAD ;
                break;
            case "price":
                sDelFolder = Config.SLASH + Config.FILE_PRICE_DEFAULT_UPLOAD;
                break;
            case "testimonial":
                sDelFolder = Config.SLASH + Config.IMAGE_TESTIMONIAL_DEFAULT_UPLOAD;
                break;
            case "banner":
                sDelFolder = Config.SLASH + Config.IMAGE_BANNER_UPLOAD;
                break;
                
        }
        string sMapPath = System.Web.HttpContext.Current.Request.MapPath("");
        string sFileDeleted = FileUpload.DeleteImageFromServer(sMapPath, sDelFolder, FileName);

        return sFileDeleted;
    }

    public static void GetStar(HtmlImage objInput, int StarRating)
    {
        switch (StarRating)
        { 
            case 1:
                objInput.Src = "../../images/1star.png";
                break;
            case 2:
                objInput.Src = "../../images/2star.png";
                break;
            case 3:
                objInput.Src = "../../images/3star.png";
                break;
            case 4:
                objInput.Src = "../../images/4star.png";
                break;
            case 5:
                objInput.Src = "../../images/5star.png";
                break;
        }
    }
    public static string UploadFile(HtmlInputFile objInput, string UploadType)
    {
     
        bool isFile = false;
        bool isSize = false;
        string sFileName = FileUpload.GetFileName(objInput.PostedFile);
        string sFileExtension = FileUpload.GetFileExtension(objInput.PostedFile);

        isFile = FileUpload.checkExtension(sFileName);
        if (isFile)
        {
            // File is OK And Check File Size
            isSize = FileUpload.checkFileSize(objInput.PostedFile, Config.FILE_LIMIT_SIZE);
            if (isSize)
            {
                // Size is OK and Upload to server
                string sMapPath =  System.Web.HttpContext.Current.Request.MapPath("");
                string sSavingFolder = "";
                
                switch (UploadType)
                {
                    case "image":
                        sSavingFolder = Config.SLASH + Config.IMAGE_NEWS_DEFAULT_UPLOAD;
                        break;
                    case "Cruise":
                        sSavingFolder = Config.SLASH + Config.IMAGE_CRUISE_DEFAULT_UPLOAD;
                        break;
                    case "file":
                        sSavingFolder = Config.SLASH + Config.FILE_DEFAULT_UPLOAD;
                        break;
                    case "trademark":
                        sSavingFolder = Config.SLASH + Config.IMAGE_TRADEMARK_DEFAULT_UPLOAD;
                        break;
                    case "product":
                        sSavingFolder = Config.SLASH + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD;
                        break;
                    case "adv":
                        sSavingFolder = Config.SLASH + Config.IMAGE_ADV_DEFAULT_UPLOAD ;
                        break;
                    case "gallery":
                        sSavingFolder = Config.SLASH + Config.IMAGE_GALLERY_DEFAULT_UPLOAD;
                        break;
                    case "tourdestination":
                        sSavingFolder = Config.SLASH + Config.IMAGE_TOURDESTINATION_UPLOAD;
                        break;
                    case "tour":
                        sSavingFolder = Config.SLASH + Config.IMAGE_TOUR_UPLOAD;
                        break;
                    case "gallerydetail":
                       string sFolder = Config.SLASH + Config.IMAGE_GALLERY_DEFAULT_UPLOAD;
                       sSavingFolder = sFolder + HttpContext.Current.Session[Config.SESSION_GALLERYID] + "/"; 
                        break;
                    case "staticpage":
                        sSavingFolder = Config.SLASH + Config.IMAGE_STATICPAGE_DEFAULT_UPLOAD ;
                        break;
                    case "price":
                        sSavingFolder = Config.SLASH + Config.FILE_PRICE_DEFAULT_UPLOAD;
                        break;
                    case "tourcat":
                        sSavingFolder = Config.SLASH + Config.IMAGE_TOURCAT_DEFAULT_UPLOAD;
                        break;
                    case "hotel":
                        sSavingFolder = Config.SLASH + Config.IMAGE_HOTEL_DEFAULT_UPLOAD;
                        break;
                    case "testimonial":
                        sSavingFolder = Config.SLASH + Config.IMAGE_TESTIMONIAL_DEFAULT_UPLOAD;
                        break;
                    case "banner":
                        sSavingFolder = Config.SLASH + Config.IMAGE_BANNER_UPLOAD;
                        break;
                }

                string sFileUploaded = FileUpload.UploadImageToServer(sMapPath, objInput.PostedFile, sFileExtension, sSavingFolder);
                if (sFileUploaded != "")
                {
                    // File uploaded
                    sFileName = sFileUploaded;
                }
                else sFileName = "";
            }
        }

        return sFileName;
    }
    public static int GetDefaultLanguage()
    {
        Languages objLang = new Languages();
        int DefaultLang = objLang.GetDefaultLanguage();
        return DefaultLang;
    }
    public static void AddStaticData(DropDownList ddlStatus, int nLanguageID)
    {
        switch (nLanguageID)
        { 
            case 1:
                ListItem all_v = new ListItem("--- Tất cả ---", "-1", true);
                all_v.Selected = true;
                ddlStatus.Items.Add(all_v);
                ListItem active_v = new ListItem("Hoạt động", "1", true);
                ddlStatus.Items.Add(active_v);
                ListItem inactive_v = new ListItem("Không hoạt động", "0", true);
                ddlStatus.Items.Add(inactive_v); 
                break;
            case 2:
                ListItem all_e = new ListItem("--- All ---", "-1", true);
                all_e.Selected = true;
                ddlStatus.Items.Add(all_e);
                ListItem active_e = new ListItem("Active", "1", true);
                ddlStatus.Items.Add(active_e);
                ListItem inactive_e = new ListItem("Inactive", "0", true);
                ddlStatus.Items.Add(inactive_e); 
                break;
            case 3:
                ListItem all_j = new ListItem("--- All ---", "-1", true);
                all_j.Selected = true;
                ddlStatus.Items.Add(all_j);
                ListItem active_j = new ListItem("Active", "1", true);
                ddlStatus.Items.Add(active_j);
                ListItem inactive_j = new ListItem("Inactive", "0", true);
                ddlStatus.Items.Add(inactive_j); 
                break;
        }
    } 
    public static void AddTourType(DropDownList ddlTourType, int nLanguageID)
    {
        switch (nLanguageID)
        {
            case 1:
                ListItem all_v = new ListItem("--- Tất cả ---", "0", true);
                all_v.Selected = true;
                ddlTourType.Items.Add(all_v);
                ListItem active_v = new ListItem("Trong nước", "1", true);
                ddlTourType.Items.Add(active_v);
                ListItem inactive_v = new ListItem("Ngoài nước", "2", true);
                ddlTourType.Items.Add(inactive_v);
                break;
            case 2:
                ListItem all_e = new ListItem("--- All ---", "0", true);
                all_e.Selected = true;
                ddlTourType.Items.Add(all_e);
                ListItem active_e = new ListItem("Indigenous Tour", "1", true);
                ddlTourType.Items.Add(active_e);
                ListItem inactive_e = new ListItem("Foreign Tour", "2", true);
                ddlTourType.Items.Add(inactive_e);
                break;
            case 3:
                ListItem all_j = new ListItem("--- All ---", "0", true);
                all_j.Selected = true;
                ddlTourType.Items.Add(all_j);
                ListItem active_j = new ListItem("Indigenous Tour", "1", true);
                ddlTourType.Items.Add(active_j);
                ListItem inactive_j = new ListItem("Foreign Tour", "2", true);
                ddlTourType.Items.Add(inactive_j);
                break;
        }
    }
    public static DataTable dtbStatus(int nLanguageID)
    {
        DataTable dt = new DataTable();
        dt.Clear();
        dt.Columns.Add("Description");
        dt.Columns.Add("Value");
        DataRow _ravi1 = dt.NewRow();
        DataRow _ravi2 = dt.NewRow();
        DataRow _ravi3 = dt.NewRow();
        switch (nLanguageID)
        {
            case 1:
                _ravi1["Description"] = "--- Tất cả ---";
                _ravi1["Value"] = "-1";
                dt.Rows.Add(_ravi1);

                _ravi2["Description"] = "Hoạt động";
                _ravi2["Value"] = "1";
                dt.Rows.Add(_ravi2);

                _ravi3["Description"] = "Không hoạt động";
                _ravi3["Value"] = "0";
                dt.Rows.Add(_ravi3);
                break;
            case 2:
                _ravi1["Description"] = "--- All ---";
                _ravi1["Value"] = "-1";
                dt.Rows.Add(_ravi1);

                _ravi2["Description"] = "Active";
                _ravi2["Value"] = "1";
                dt.Rows.Add(_ravi2);

                _ravi3["Description"] = "Inactive";
                _ravi3["Value"] = "0";
                dt.Rows.Add(_ravi3);
                break;
            case 3:
                _ravi1["Description"] = "--- All ---";
                _ravi1["Value"] = "-1";
                dt.Rows.Add(_ravi1);

                _ravi2["Description"] = "Active";
                _ravi2["Value"] = "1";
                dt.Rows.Add(_ravi2);

                _ravi3["Description"] = "Inactive";
                _ravi3["Value"] = "0";
                dt.Rows.Add(_ravi3);
                break;
        }
        return dt;
    }
    public static DataTable dtbTourType(int nLanguageID)
    {
        DataTable dt = new DataTable();
        dt.Clear();
        dt.Columns.Add("Description");
        dt.Columns.Add("Value");
        DataRow _ravi1 = dt.NewRow();
        DataRow _ravi2 = dt.NewRow();
        DataRow _ravi3 = dt.NewRow();
        switch (nLanguageID)
        {
            case 1:
                _ravi1["Description"] = "--- Tất cả ---";
                _ravi1["Value"] = "0";
                dt.Rows.Add(_ravi1);

                _ravi2["Description"] = "Trong nước";
                _ravi2["Value"] = "1";
                dt.Rows.Add(_ravi2);

                _ravi3["Description"] = "Ngoài nước";
                _ravi3["Value"] = "2";
                dt.Rows.Add(_ravi3);
                break;
            case 2:
                _ravi1["Description"] = "--- All ---";
                _ravi1["Value"] = "0";
                dt.Rows.Add(_ravi1);

                _ravi2["Description"] = "Indigenous Tour";
                _ravi2["Value"] = "1";
                dt.Rows.Add(_ravi2);

                _ravi3["Description"] = "Foreign Tour";
                _ravi3["Value"] = "2";
                dt.Rows.Add(_ravi3);
                break;
            case 3:
                _ravi1["Description"] = "--- All ---";
                _ravi1["Value"] = "0";
                dt.Rows.Add(_ravi1);

                _ravi2["Description"] = "Indigenous Tour";
                _ravi2["Value"] = "1";
                dt.Rows.Add(_ravi2);

                _ravi3["Description"] = "Foreign Tour";
                _ravi3["Value"] = "2";
                dt.Rows.Add(_ravi3);
                break;
        }
        return dt;
    }
    public static int GetCurrentLanguage()
    {
        int nCurrentLang = 2;
        try
        {
            nCurrentLang = Convert.ToInt32(HttpContext.Current.Session["CurrentLanguage"]);
            if (nCurrentLang == 0)
                nCurrentLang = GetDefaultLanguage();
        }
        catch { nCurrentLang =2; }
        return nCurrentLang;
    }
   
    public static  void setValueToCookie(string Name, string Value,HttpContext Context)
    {
        HttpCookie myCookie = new HttpCookie(Name);
        myCookie.Expires = DateTime.Now.AddDays(45);
        myCookie.Value = Value;
        Context.Response.Cookies.Add(myCookie);
    }

    public static string getValueFromCookie(string Name, HttpContext Context)
    {
        string strResult = "";
        try
        {
            HttpCookie myCookie;
            myCookie = (HttpCookie) Context.Request.Cookies[Name] ;
            strResult = myCookie.Value.ToString();
        }
        catch
        {
            strResult = "";
        }
        return strResult;
    }

    public static string CreateConfirmBoxClient(string msg)
    {
        string strResult = "return confirm('{0}');";
        strResult = string.Format(strResult, msg);
        return strResult;
    }

    public static string getUserEmail(HttpContext context)
    {
        string sResult = "";
        try
        {
            sResult = Convert.ToString(context.Session[Config.SESSION_USEREMAIL]);
        }
        catch
        {
            sResult = "";
        }
        return sResult;
    }

    public static string getUserName(HttpContext context)
    {
        string sResult = "";
        try
        {
            sResult = Convert.ToString(context.Session[Config.SESSION_USERNAME]);
        }
        catch
        {
            sResult = "";
        }
        return sResult;
    }
   
    public static int GetTotalPage(int _PageSize, int _TotalItem)
    {
        int DivTP = _TotalItem % _PageSize;
        int RealTP = _TotalItem / _PageSize;
        if (DivTP > 0)
            return RealTP + 1;
        else
            return RealTP;
    }
    public static string FormatCurrency(object value)
    {
        string strFormat = "{0:0,0}";
        decimal decValue = ConvertData.ConvertToDecimal(value);
        return string.Format(strFormat, value);
    }
}

public class Utl
{
    public static void SetNonSelectToControl(DropDownList control, string CaptionSelectOne, string CaptionSelectNoAvailble)
    {
        if(control.Items.Count >0)
            control.Items.Insert(0, new ListItem(CaptionSelectOne, "-1"));
        else
            control.Items.Insert(0, new ListItem(CaptionSelectNoAvailble, "-1"));
    }
}



